﻿using System;
using Microsoft.Practices.Unity;
using UnityDemo.InstanceManagerCode.LifeTimeModels;
using UnityDemo.InstanceManagerCode.LifetimeTypes;
using UnityDemo.Models;

namespace UnityDemo.InstanceManagerCode
{
    //ContainerControlledLifetimeManager : One object per unity container instance (including childrens)
    // HierarchicalLifetimeManager : One object per unity child container
    // PerResolveLifetimeManager : A new object per call to resolve, recursive dependencies will reuse objects.
    // PerThreadLifetimeManager : One new object per thread
    // ExternallyControlledLifetimeManager: Moves the control outside of Unity
    //http://msdn.microsoft.com/en-us/library/ff647854.aspx
    //http://www.ladislavmrnka.com/2011/03/unity-build-in-lifetime-managers/
    //http://msdn.microsoft.com/en-us/library/microsoft.practices.unity.lifetimemanager.aspx
    //1.Transient                      http://msdn.microsoft.com/en-us/library/microsoft.practices.unity.transientlifetimemanager.aspx
    //2.ContainerControled
    //3.ExternallyControled            http://msdn.microsoft.com/en-us/library/microsoft.practices.unity.externallycontrolledlifetimemanager.aspx
    //4.PerThread                      http://msdn.microsoft.com/en-us/library/microsoft.practices.unity.perthreadlifetimemanager.aspx
    //5.SynchronizedLifeTimeManager    http://msdn.microsoft.com/en-us/library/microsoft.practices.unity.synchronizedlifetimemanager.aspx
    //6/HierarchicalLifetimeManager    http://msdn.microsoft.com/en-us/library/ff660872(v=PandP.20).aspx
    public class LifeTimeDemo : BaseFeatureDemo
    {
        // In this sample I've used Printed the hash code of each instance to show its uniqueness
        public LifeTimeDemo()
        {
            var container = new UnityContainer();
            container.RegisterType<SingletonInstance>(new ContainerControlledLifetimeManager());
            container.RegisterType<PerInstance>();
            container.RegisterType<ExternalInstanceLifetime>(new ExternallyControlledLifetimeManager());
            container.RegisterType<PerThredInstance>(new PerThreadLifetimeManager());
            container.RegisterType<HierarchicalLifetimeInstance>(new HierarchicalLifetimeManager());

            _sampleCollection[1].ShowValue();
        }

        protected override void RegisterSamples()
        {
            _sampleCollection[1] = new Lazy<ISample>(() => new TransientLifetimeManagerDemo());

            _sampleCollection[2] = new Lazy<ISample>(() => new ContainerConrtolledLifeTimeManagerDemo());

            _sampleCollection[3] = new Lazy<ISample>(() => new ExternallyControlledLifetimeManagerSample());

            _sampleCollection[4] = new Lazy<ISample>(() => new PerThreadLifetimeManagerDemo());

            _sampleCollection[5] = new Lazy<ISample>(() => new HierarchicalLifetimeManagerDemo());
        }
    }
}